const express = require('express');
const multer = require('multer');
const path = require("path");
const Result = require('../models/Result');
const { UPLOAD_PATH ,UPLOAD_URL } = require('../utils/constant')
const { decoded } = require("../utils");
const  adminUserServices  = require('../services/admin_user');
const Boom = require('boom');


const router = express.Router();



router.post(//删除用户
    '/removeUser',
    function(req,res){
        const decode = decoded(req)
        if(decode && decode.username){
            adminUserServices.removeUser(req.body).then(data=>{
                new Result(null,'删除成功').success(res)
            })
        }
    }
)




function endThanTotay(endTime){
    var enddate = new Date((endTime).replace(/-/g,"/"));  
    var nowdate = new Date();
    if(nowdate > enddate)    
    {
        return 0;
    }
    else
    {
       return 1;
    }
}

router.post(//获取用户列表
    '/getUserList',
    function(req,res){
        const decode = decoded(req)
        if(decode && decode.username){
            
            adminUserServices.getUserList().then(data=>{

                // data.forEach((element,index) => {   
                //     var state = endThanTotay(element.over_time)    
                //     data[index].state = state
                // });
                


                new Result(data,'请求成功').success(res)
            })
        }
    }
)


router.post('/getUser',function(req,res){//获取用户详情
    const decode = decoded(req)
    if(decode && decode.username){
        adminUserServices.getCreate(req.body).then((data)=>{
            new Result(data,'查询成功').success(res)
        }).catch(err=>{
            next(Boom.badImplementation(err))
        })
    }
})



router.post('/updataUser',function(req,res,next){//更新用户
    const decode = decoded(req)
    if(decode && decode.username){
        console.log(req.body)
        adminUserServices.updataCentre(req.body).then(()=>{
            new Result(null,'更新成功').success(res)
        }).catch(err=>{
            next(Boom.badImplementation(err))
        })
    }
})


module.exports = router